package com.ruoyi.nongjt.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.nongjt.domain.PrinterAuth;
import com.ruoyi.nongjt.mapper.PrinterAuthMapper;
import com.ruoyi.nongjt.service.IPrinterAuthService;

/**
 * 打印机认证Service实现
 * 
 * @author ruoyi
 * @date 2024-04-05
 */
@Service
public class PrinterAuthServiceImpl implements IPrinterAuthService
{
    @Autowired
    private PrinterAuthMapper printerAuthMapper;

    /** 认证有效期（天） */
    private static final int AUTH_EXPIRE_DAYS = 365;

    /**
     * 新增打印机认证
     */
    @Override
    public int insertPrinterAuth(PrinterAuth printerAuth)
    {
        // 设置认证时间和过期时间
        printerAuth.setAuthTime(new Date());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_YEAR, AUTH_EXPIRE_DAYS);
        printerAuth.setCreateTime(calendar.getTime());
        printerAuth.setAuthStatus(0); // 0：正常
        
        return printerAuthMapper.insertPrinterAuth(printerAuth);
    }

    /**
     * 修改打印机认证
     */
    @Override
    public int updatePrinterAuth(PrinterAuth printerAuth)
    {
        return printerAuthMapper.updatePrinterAuth(printerAuth);
    }

    /**
     * 查询打印机认证列表
     */
    @Override
    public List<PrinterAuth> selectPrinterAuthList(PrinterAuth printerAuth)
    {
        // 设置当前用户只能查看自己的认证记录
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            printerAuth.setUserId(SecurityUtils.getUserId());
        }
        return printerAuthMapper.selectPrinterAuthList(printerAuth);
    }

    /**
     * 通过用户ID查询打印机认证记录
     */
    @Override
    public List<PrinterAuth> selectPrinterAuthByUserId(Long userId)
    {
        PrinterAuth printerAuth = new PrinterAuth();
        printerAuth.setUserId(userId);
        return printerAuthMapper.selectPrinterAuthList(printerAuth);
    }

    /**
     * 通过认证ID查询打印机认证
     */
    @Override
    public PrinterAuth selectPrinterAuthByAuthId(Long authId)
    {
        return printerAuthMapper.selectPrinterAuthByAuthId(authId);
    }

    /**
     * 通过序列号查询打印机认证
     */
    @Override
    public PrinterAuth selectPrinterAuthBySerialNumber(String serialNumber)
    {
        return printerAuthMapper.selectPrinterAuthBySerialNumber(serialNumber);
    }

    /**
     * 打印机认证
     */
    @Override
    public boolean printerAuth(Long userId, String serialNumber, String authCode)
    {
        // 检查是否已经认证过该打印机
        PrinterAuth existingAuth = printerAuthMapper.selectPrinterAuthBySerialNumber(serialNumber);
        if (existingAuth != null) {
            if (existingAuth.getUserId().equals(userId)) {
                existingAuth.setAuthTime(new Date());
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.add(Calendar.DAY_OF_YEAR, AUTH_EXPIRE_DAYS);
                existingAuth.setCreateTime(calendar.getTime());
                existingAuth.setAuthStatus(0); // 0：正常
                printerAuthMapper.updatePrinterAuth(existingAuth);
                return true;
            } else {
                // 该打印机已经被其他用户认证
                return false;
            }
        }
        
        // 创建新的认证记录
        PrinterAuth newAuth = new PrinterAuth();
        newAuth.setUserId(userId);
        newAuth.setPrinterSerial(serialNumber);
        newAuth.setAuthTime(new Date());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_YEAR, AUTH_EXPIRE_DAYS);
        newAuth.setCreateTime(calendar.getTime());
        newAuth.setAuthStatus(0); // 0：正常
        
        return printerAuthMapper.insertPrinterAuth(newAuth) > 0;
    }

    /**
     * 取消打印机认证
     */
    @Override
    public int cancelPrinterAuth(Long authId)
    {
        return printerAuthMapper.deletePrinterAuthByAuthId(authId);
    }

    /**
     * 批量删除打印机认证
     */
    @Override
    public int deletePrinterAuthByAuthIds(Long[] authIds)
    {
        return printerAuthMapper.deletePrinterAuthByAuthIds(authIds);
    }

    /**
     * 删除打印机认证信息
     */
    @Override
    public int deletePrinterAuthByAuthId(Long authId)
    {
        return printerAuthMapper.deletePrinterAuthByAuthId(authId);
    }
}